﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using TRicheControl.Comm;
using TRicheControl.Designer;
using TRicheControl.Editor;

namespace TRicheControl.WinformControl
{
    [ToolboxBitmap(typeof(ToolBar))]
    [Designer(typeof(RicheMenuBarDesigner))]
    public partial class RicheMenuBar : Control
    {
        private Graphics mGraphics;
        public Graphics Graphics
        {
            get
            {
                if (base.IsDisposed)
                {
                    return null;
                }
                if (mGraphics == null)
                {
                    mGraphics = base.CreateGraphics();
                }
                return mGraphics;
            }
        }

        private RicheMenuBarItemCollection mBarItems ;

        [Editor(typeof(RicheMenuBarItemEditor), typeof(UITypeEditor))]
        [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Bindable(true)]
        public RicheMenuBarItemCollection BarItems
        {
            get
            {
                return mBarItems;
            }
            set
            {
                mBarItems = value;
            }
        }

        private List<Rectangle> mBarItemRects = new List<Rectangle>();

        public RicheMenuBar()
        {
            InitializeComponent();
            this.Dock = DockStyle.Top;
            mBarItems = new RicheMenuBarItemCollection();
            BarItems.OnItemChangeEvent += BarItems_OnItemChangeEvent;
        }

        private void BarItems_OnItemChangeEvent(ItemChangeType changeType, int index)
        {
            if (changeType == ItemChangeType.Add)
            {
                this.Controls.Add(BarItems[index]);
            }
            else
            {
                this.Controls.RemoveAt(index);
            }
            ResetBarItemLocation(index);
        }

        private void ResetBarItemLocation(int changeIndex)
        {
            if (changeIndex != 0 && BarItems.Count <= changeIndex)
                return;
            mBarItemRects.RemoveRange(changeIndex, mBarItemRects.Count - changeIndex);

            for (int index = changeIndex; index < BarItems.Count; index++)
            {
                Size rectSize = BarItems[index].GetSize();
                rectSize.Height = this.Height;
                Rectangle rect = new Rectangle();
                rect.Size = rectSize;
                if (index == 0)
                {
                    rect.X = this.Padding.Left;
                    rect.Y = 0;
                }
                else
                {
                    rect.X = mBarItemRects[index - 1].X + rect.Width + this.Padding.Left;
                    rect.Y = 0;
                }
                BarItems[index].SetBounds(rect.X, rect.Y, rect.Width, rect.Height);
                mBarItemRects.Add(rect);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            DrawMenuBar(pe.Graphics);
            PaintEventArgs pe1 = new PaintEventArgs(pe.Graphics, Rectangle.Empty);
        }
        public virtual void OnItemAdded(RicheMenuBarButtonItem Item)
        {

        }
        protected virtual void OnDrawItem(PaintEventArgs pe)
        {

        }
        protected override void OnResize(EventArgs e)
        {
            DrawMenuBar(this.Graphics);
            base.OnResize(e);
        }
        RicheMenuBarItem mMouseInItem;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            RicheMenuBarItem barItem = GetMouseFoucedMenuBarItem(e.Location);
            if (barItem != null)
            {
                RicheMenuBarItem mbi = mMouseInItem;
                mMouseInItem = barItem;
                if (mbi != null)
                {
                    mbi.Refresh();
                }
                barItem.Refresh();
            }
            base.OnMouseMove(e);
        }
        private void DrawMenuBar(Graphics g)
        {
            GraphicsStyle.Styles.MenuBarStyle.DrawBarBorder(g, this.Bounds);
            GraphicsStyle.Styles.MenuBarStyle.DrawBackColor(g, this.Bounds, this.BackColor);
        }

        private RicheMenuBarItem GetMouseFoucedMenuBarItem(Point loction)
        {
            foreach (RicheMenuBarItem barItem in mBarItems)
            {
                if (loction.X >= barItem.Bounds.X && loction.X <= barItem.Bounds.X + barItem.Bounds.Width)
                {
                    return barItem;
                }
            }
            return null;
        }
    } 
}
